<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px" label-position="left">
      <el-form-item label="专栏名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="专栏名称"></el-input>
      </el-form-item>
      <el-form-item label="封面图" prop="pic">
        <div class="bannerImg-wrapper pic">
          <el-upload
            ref="uploadBannerImg"
            :action="uploadBannerImgUrl"
            :show-file-list="true"
            :on-success="handleBannerImgSuccess"
            :on-error="handleBannerImgError"
            :before-upload="beforeBannerImgUpload">
            <img v-if="dataForm.pic" :src="dataForm.pic" class="bannerImg">
            <i v-else class="el-icon-plus bannerImg-uploader-icon"></i>
          </el-upload>
        </div>
      </el-form-item>
      <el-form-item label="专栏简介" prop="summary">
        <el-input v-model="dataForm.summary" type="textarea" :rows="5" maxlength="240" placeholder="240字内"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        bannerImgErrorTip: '',
        uploadBannerImgUrl: this.$http.adornUrl(`/sys/oss/upload?token=${this.$cookie.get('token')}`),
        dataForm: {
          code: '',
          name: '',
          pic: '',
          summary: ''
        },
        dataRule: {
          name: [
            {required: true, message: '专栏名称不能为空', trigger: 'blur'}
          ],
          pic: [
            {required: true, message: '封面图不能为空', trigger: 'blur'}
          ],
          summary: [
            {required: true, message: '简介不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/picbook/pbcolumn/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.data.pbColumn
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/picbook/pbcolumn/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                pbColumn: this.dataForm
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  // duration: 1500,
                  duration: 500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      handleBannerImgSuccess (res, file) {
        this.dataForm.pic = res.url
        let _this = this
        setTimeout(function () {
          _this.$refs.uploadBannerImg.clearFiles()
        }, 1000)
      },
      beforeBannerImgUpload (file) {
        if (file.type.indexOf('image') < 0) {
          this.$message.error('图片格式不正确')
          return false
        }
      },
      handleBannerImgError (err, file, fileList) {
        this.$message.error('上传失败')
      },
    }
  }
</script>
<style lang="less" scoped>
  .bannerImg-wrapper {
    .bannerImg-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      border: 1px solid #d9d9d9;
      border-radius: 6px;
      width: 266px;
      height: 150px;
      line-height: 150px;
      text-align: center;
    }
    .bannerImg {
      width: 266px;
      height: 150px;
      display: block;
    }
  }

  .error-tip {
    color: #f56c6c;
    font-size: 12px;
    line-height: 1;
    padding-top: 4px;
  }

</style>
